#!/bin/bash
echo "Begin"
#创建网络
docker -v
if [[ `docker network ls | grep "jy_net"` == "" ]]; then
  echo "step: creat network jy_net"
  docker network create --driver bridge --subnet 172.200.0.0/16 --gateway 172.200.0.1 jy_net 
fi
########
#portainer
pname="myPortainer" #容器名
docker inspect $pname -f '{{.Name}}' > /dev/null
if [ $? -ne 0 ] ;then
  portainerdir="/home/portainer"
  if [ ! -d $portainerdir ];then
    echo "step: creat chinese portainer dir"
    mkdir $portainerdir
  fi
  if [[ `command -v 7za` == "" ]];then  
    echo "step: pre chinese file"
    yum install -y epel-release
    yum install -y p7zip
    #下载汉化包
    if [ ! -d $portainerdir/public ]; then
      echo "step: download chinese file"
      wget -N -P $portainerdir https://gitee.com/yifff/cent-os-init/attach_files/887602/download/public.7z
      if [ $? -ne 0 ]; then
        echo "wget public fail exit"
        exit 8
      fi
      echo "step: 7z chinese file"
      7za x $portainerdir/public.7z -r -o$portainerdir/
    fi
  fi
  #创建虚拟盘
  if [[ `docker volume ls | grep "portainer_data"` == "" ]]; then
    echo "step: creat docker volume portainer_data"
    docker volume create portainer_data
  fi
  #下载指定版本
  if [[ `docker images | grep "portainer/portainer"` == "" ]]; then 
    echo "step: docker pull image portainer"
    docker pull portainer/portainer:linux-amd64-1.20.2
  fi
  echo "step: run docker image portainer"
  docker run --name $pname -d --restart=always -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data -v /home/portainer/public:/public portainer/portainer:linux-amd64-1.20.2
fi
######

#dockercompose
bindir="/usr/local/bin"
if [[ `command -v docker-compose` == "" ]];then
  echo "setp: download docker-compose"
  wget -N -O $bindir/docker-compose https://gitee.com/yifff/cent-os-init/attach_files/895370/download/docker-compose-linux-x86_64
  chmod +x $bindir/docker-compose
fi


echo "end "


